#!/usr/bin/python

from visual import *
from sys import stdout

#scene.lights = [distant_light(direction=(0, -1, 0),color=color.gray(0.5)), distant_light(direction=(0,  1, 0),color=color.gray(0.5))]
#scene.lights += (distant_light(direction=(0, -1, 0),color=color.gray(0.5)), distant_light(direction=(0,  1, 0),color=color.gray(0.5)))
x = """scene.lights = (vector(+1,0,0),
				vector(-1,0,0),
				vector(0,+1,0),
				vector(0,-1,0),
				vector(0,0,+1),
				vector(0,0,-1),
				)"""
scene.ambient = 0.5
#scene.ambient = color.gray(0.5)
#scene.ambient = 128
#scene.stereo = 'crosseyed'
scene.stereo = 'redblue'
scene.stereodepth = 2

currentTurn = 0

layers = []
for z in range(5):
	layer = []
	layer.append(curve(pos=[(-.5,z,-.5),(-.5,z,7.5),(7.5,z,7.5),(7.5,z,-.5)]))
	for y in range(8):
		layer.append(curve(pos=[(-.5,z,y-.5),(7.5,z,y-.5)]))
		for x in range(8):
			layer.append(curve(pos=[(x-.5,z,-.5),(x-.5,z,7.5)]))
		for x in range(8):
			if ((y+x)%2):
				col = (1,1,1)
			else:
				col = (0,0,0)
#			box(pos=(x,z-0.01,y),length=1,height=0.01,width=1,color=col,opacity=0.1)
	layers.append(layer)

#for layer in layers:
#	for c in layer:
#		c.visible = false

scene.center = (3.5,3.5,3.5)

nx = 3
ny = 2
nt = 2
box(pos=(nx,nt-0.001,ny),length=1,height=0.001,width=1,color=(0,0,1),opacity=0.2)
scene.center = (nx,nt,ny)

knight = frame()
box(frame=knight, pos=(0,.4,0),length=.3,height=0.8,width=.3)
box(frame=knight, pos=(0,.5,.2),length=.5,height=.3,width=.3,axis=(0,-1,1))
knight.pos = (nx,nt,ny)

pawn = frame()
cone(frame=pawn, pos=(0,0,0), axis=(0,.5,0), radius=.3)
sphere(frame=pawn, pos=(0,.5,0), radius=.15)
pawn.pos = (nx+2,nt+1,ny)

scene.center = pawn.pos

rook = frame()
cylinder(frame=rook, pos=(0,0,0), axis=(0,.7,0), radius=.3 )
crensize = .15
box(frame=rook, pos=(+.3-(crensize/2),.7+(crensize/2),0), width=crensize, height=crensize, length=crensize)
box(frame=rook, pos=(-.3+(crensize/2),.7+(crensize/2),0), width=crensize, height=crensize, length=crensize)
box(frame=rook, pos=(0,.7+(crensize/2),+.3-(crensize/2)), width=crensize, height=crensize, length=crensize)
box(frame=rook, pos=(0,.7+(crensize/2),-.3+(crensize/2)), width=crensize, height=crensize, length=crensize)
rook.pos = (nx-2,nt+1,ny)

king = frame()
cylinder(frame=king, pos=(0,0,0), axis=(0,.7,0), radius=.2 )
box(frame=king, pos=(0,.85,0), width=.1, height=.1, length=.3, axis=(1,0,0))
box(frame=king, pos=(0,.85,0), width=.1, height=.1, length=.3, axis=(0,1,0))
king.pos = (nx-1,nt+2,ny)

queen = frame()
cylinder(frame=queen, pos=(0,0,0), axis=(0,.7,0), radius=.2 )
cone(frame=queen, pos=(0,.9,0), axis=(0,-.6,0), radius=.3)
sphere(frame=queen, pos=(0,.9,0), radius=.05)
queen.pos = (nx,nt+2,ny)

bishop = frame()
cylinder(frame=bishop, pos=(0,0,0), axis=(0,.6,0), radius=.15)
cone(frame=bishop, pos=(0,.65,0), axis=(0,.3,0), radius=.2)
cone(frame=bishop, pos=(0,.65,0), axis=(0,-.3,0), radius=.2)
bishop.pos = (nx+2,nt,ny+1)


movelist = [
			(nx+1,ny+2,nt  ),
			(nx+1,ny-2,nt  ),
			(nx-1,ny+2,nt  ),
			(nx-1,ny-2,nt  ),
			(nx+2,ny+1,nt  ),
			(nx+2,ny-1,nt  ),
			(nx-2,ny+1,nt  ),
			(nx-2,ny-1,nt  ),

			(nx  ,ny+1,nt+2),
			(nx  ,ny+1,nt-2),
			(nx  ,ny-1,nt+2),
			(nx  ,ny-1,nt-2),
			(nx  ,ny+2,nt+1),
			(nx  ,ny+2,nt-1),
			(nx  ,ny-2,nt+1),
			(nx  ,ny-2,nt-1),

			(nx+1,ny  ,nt+2),
			(nx+1,ny  ,nt-2),
			(nx-1,ny  ,nt+2),
			(nx-1,ny  ,nt-2),
			(nx+2,ny  ,nt+1),
			(nx+2,ny  ,nt-1),
			(nx-2,ny  ,nt+1),
			(nx-2,ny  ,nt-1),
			]
for ax, ay, at in movelist:
	x, y, z = (ax,ay,at)
#	box(pos=(x,z+0.5,y),length=1,height=1,width=1,color=(1,0,0),opacity=0.2)
	box(pos=(x,z-0.001,y),length=1,height=0.001,width=1,color=(1,0,0),opacity=1)

pieceSelected = None
while True:
	rate(20)
	while scene.mouse.clicked:
		evt = scene.mouse.getclick()

		if scene.mouse.pick:
			selected = scene.mouse.pick.frame
		else:
			selected = None
		if selected:
			if pieceSelected:
				for obj in pieceSelected.objects:
					obj.color = (1,1,1)
			pieceSelected = selected
			for obj in pieceSelected.objects:
				obj.color = (0,1,0)
				scene.center = selected.pos
		elif pieceSelected and scene.mouse.pick:
			pieceSelected.pos = scene.mouse.pick.pos
			for obj in pieceSelected.objects:
				obj.color = (1,1,1)
			pieceSelected = None
#		for square in thisBoard.squares:
#			if square:
#				if type(square.base) is frame and scene.mouse.pick in square.base.objects:
#					for obj in square.base.objects:
#						obj.color=(0,0,255)
#				elif scene.mouse.pick is square.base:
#					square.base.color=(0,0,255)

